package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"warehouse/models"
)

type WarehouseController struct {
	beego.Controller
}

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *WarehouseController) List() {
	c.Ctx.Output.Header("", "*")
	var ware []models.Warehouse
	err := models.DB.Find(&ware).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "数据查询失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "数据查询成功",
			Data: ware,
		}
		c.ServeJSON()

	}

}

func (c *WarehouseController) Add() {
	c.Ctx.Output.Header("", "*")
	name := c.GetString("name")
	tel := c.GetString("tel")
	address := c.GetString("address")
	ware := models.Warehouse{
		Name:    name,
		Tel:     tel,
		Address: address,
	}
	err := models.DB.Find(&ware).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "数据添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "数据添加成功",
			Data: ware,
		}
		c.ServeJSON()

	}
}
